package com.read.admin.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name="loginServlet",urlPatterns={"/login.do","/unlogin.do"})
public class LoginServlet extends HttpServlet{
	
	private static final long serialVersionUID = -2724513761394261208L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String url = req.getRequestURI();
		if(url.endsWith("login.do")){
			login(req, resp);
		}
		if(url.endsWith("unlogin.do")){
			unlogin(req, resp);
		}
	}
	
	private void login(HttpServletRequest req, HttpServletResponse resp){
		String username = req.getParameter("username");
		String passwd = req.getParameter("passwd");
		HttpSession session = req.getSession();
		if(username.equals("read") && passwd.equals("123456")){
			session.setAttribute("sessionKey", username);
			try {
				resp.sendRedirect(req.getContextPath()+"/main.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			try {
				resp.getWriter().println("用户名或密码错误");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private void unlogin(HttpServletRequest req, HttpServletResponse resp){
		
	}
	
}
